package com.example.a1.zyfypt_322jzy.model;


import com.example.a1.zyfypt_322jzy.iface.RegisterIface;
import com.example.a1.zyfypt_322jzy.iface.RegisterListener;
import com.example.a1.zyfypt_322jzy.service.RegisterService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 *
 *
 * 获取注册结果
 */

public class RegisterModel implements RegisterIface {

    private Retrofit retrofit;

    private String BASEURL = "http://amicool.neusoft.edu.cn/";

    public RegisterModel(){
        retrofit = new Retrofit.Builder().baseUrl(BASEURL).addConverterFactory(ScalarsConverterFactory.create()).build();
    }

    @Override
    public void getResult(String username, String password, String tel, final RegisterListener registerListener) {
        RegisterService registerService = retrofit.create(RegisterService.class);
        Call<String> call = registerService.reg(username,password,tel);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful()&&response!=null)
                {
                    System.out.println("success 注册");
                    registerListener.onResponse(response.body());
                }
                else{
                    System.out.println("失败 注册");
                    registerListener.onFail("onresponse fail");
                }

            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                registerListener.onFail(t.toString());
            }
        });
    }

}
